/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package stattracker;

import javax.swing.JLabel;

import java.io.File;
import java.io.Serializable;

/**
 *
 * @author mattcrain
 */
public class Gamertag implements Comparable<Gamertag>, Serializable
{
	private static final long serialVersionUID = 1L;
	private String name;
    private JLabel avatar;
    
    public Gamertag(String name)
    {
        this.name = name;
        setAvatar();
    }
    
    public String getGamertag()
    {
        return name;
    }
    
    public JLabel getAvatar()
    {
        return avatar;
    }
    
    private void setAvatar()
    {
        String avatarURL = "http://avatar.xboxlive.com/avatar/" + name.replaceAll(" ", "%20")
                + "/avatar-body.png";
        try
        {
           avatar = MyUtils.getImage(avatarURL, "MedalIcons/defaultAvatar.png");
        }
        catch(Exception e)
        {
        	avatar = new JLabel(MyUtils.createImageIcon("MedalIcons/defaultAvatar.png", ""));
        }
    }
    
    public void updateAvatar()
    {
        setAvatar();
    }
    
    public boolean hasHaloFile()
    {
    	return new File(getGamertag() + ".halo3").exists();
    }

    public int compareTo(Gamertag other)
    {
    	if(hasHaloFile())
    	{
    		if(other.hasHaloFile())
    			return this.name.toLowerCase().compareTo(other.getGamertag().toLowerCase());
    		else
    			return -1;
    	}
    	else
    	{
    		if(other.hasHaloFile())
    			return 1;
    		else
    			return this.name.toLowerCase().compareTo(other.getGamertag().toLowerCase());
    	}
    }
}
